﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using webSocket;

public class WebSocketByteBuffer
{
    MarkCompressInput input;
    MarkCompressOutput output;
    public WebSocketByteBuffer New()
    {
        return new WebSocketByteBuffer();
    }
    public WebSocketByteBuffer New(byte[] data)
    {
        return new WebSocketByteBuffer(data);
    }
    public WebSocketByteBuffer()
    {
        Byte writebuffer = new Byte();
        writebuffer.endian = Byte.BIG_ENDIAN;
        writebuffer.length = 0;
        writebuffer.pos = 0;
        output = MarkCompressOutput.create(writebuffer);
    }
    public WebSocketByteBuffer(byte[] data)
    {
        Byte readBuffer = new Byte(new List<byte>(data));
        readBuffer.endian = Byte.BIG_ENDIAN;
        readBuffer.pos = 0;
        input = MarkCompressInput.create(readBuffer);
    }
    public byte[] GetWriteBuffer()
    {
        Byte buffer = output.Out;
        if (buffer.length > MessageProtocol.MESSAGE_MAX)
        {
            throw new ProtocolException("严重错误，消息长度超过最大值:" + MessageProtocol.MESSAGE_MAX);
        }
        buffer.pos = 0;
        byte[] byteArr = buffer.buffer.ToArray();
        return byteArr;
    }
    //---------------------------------------------------------------------------------------------------------------
    public void writeBoolean(bool v)
    {
        output.writeBoolean(v);
    }
    public void writeInt(int v)
    {
        output.writeInt(v);
    }
    public void writeLong(long v)
    {
        output.writeLong(v);
    }
    public void writeFloat(float v)
    {
        output.writeFloat(v);
    }
    public void writeDouble(double v)
    {
        output.writeDouble(v);
    }
    public void writeString(string s)
    {
        output.writeString(s);
    }
    public void writeBooleanArray(bool[] v)
    {
        output.writeBooleanArray(v);
    }
    public void writeIntArray(int[] v)
    {
        output.writeIntArray(v);
    }
    public void writeLongArray(long[] v)
    {
        output.writeLongArray(v);
    }
    public void writeFloatArray(float[] v)
    {
        output.writeFloatArray(v);
    }
    public void writeDoubleArray(double[] v)
    {
        output.writeDoubleArray(v);
    }
    public void writeStringArray(string[] v)
    {
        output.writeStringArray(v);
    }
    //---------------------------------------------------------------------------------------------------------------
    public bool readBoolean()
    {
        return input.readBoolean();
    }
    public int readInt()
    {
        return input.readInt();
    }
    public long readLong()
    {
        return input.readLong();
    }
    public float readFloat()
    {
        return input.readFloat();
    }
    public string readString()
    {
        return input.readString();
    }
    public bool[] readBooleanArray()
    {
        return input.readBooleanArray();
    }
    public int[] readIntArray()
    {
        return input.readIntArray();
    }
    public long[] readLongArray()
    {
        return input.readLongArray();
    }
    public float[] readFloatArray()
    {
        return input.readFloatArray();
    }
    public string[] readStringArray()
    {
        return input.readStringArray();
    }
}
